console.log('myRTC.js :-)');

myRTC={}; // get organized here

if(document.location.search.length>0){ // this is a client
	myRTC.server=document.location.search.slice(1);
} else{ // this is a server
	myRTC.server=false;
}


myRTC.showmsg=function(msg){
	var p = document.createElement('p');
	myRTC.showmsg.i++;
	p.innerHTML=myRTC.showmsg.i+') '+msg;
	listening.insertBefore(p,listening.children[0]);
};
myRTC.showmsg.i=0;

send2all.hidden=true;send2dest.hidden=true; // hide send box for now // hide send box for now

myRTC.send=function(to,msg,el){ // send message to
	var conn = myRTC.peer.connect(to); // everybody listens to lala0
	conn.on('open', function(){
		conn.send(msg);
		console.log(' [to '+to+'] > '+msg);
		myRTC.showmsg(' [to '+to+'] > '+msg);
		if(!!el){el.value=""}; // if provided, clear teh value of the sending input element
		//sendmsg.value="";
  	});
  	myRTC.checkAll();
	return conn;
};

myRTC.checkAll=function(){
	var conn = myRTC.peer.connect(myRTC.server);
	conn.on('open', function(){
		send2all.hidden=false
  	});
  	conn.on('close', function(){
		send2all.hidden=true
  	});

  	// check connections
  	myRTC.connections = Object.getOwnPropertyNames(myRTC.peer.managers);
  	checkAll.innerHTML="";
  	for(var c in myRTC.connections){
  		var cc=myRTC.connections[c];
  		if(cc!==myRTC.server){
  			var el = document.createElement('button');
  			el.textContent=cc;
  			checkAll.appendChild(el);
  			el.onclick=function(){
  				destId.value=this.textContent;
  			}
  		}
  		
  	}

	return conn;
};

// Identification 1st
myId.focus(); 
myId.onkeyup=function(evt){
	if(evt.keyCode==13){
		var mid = myId.value; // myId
		if(!myRTC.server){
			myRTC.server=mid;
			myId.disabled=true;
			document.getElementById('more').innerHTML=' this is a server now,<br>create clients with <a href="http://bstation.googlecode.com/git/webRTC.html?'+mid+'" target=_blank>http://bstation.googlecode.com/git/webRTC.html?'+mid+'</a>';
		}

		//send2all.hidden=false;
		send2dest.hidden=false;
		sendmsgtoall.focus(); // next change focus to sending messages
		var peer = new Peer(mid,{key: 'hpg8gu4segsyvi'}); // <-- my connection
		myRTC.peer=peer;
		myRTC.checkAll();
		myRTC.t = setInterval(myRTC.checkAll,3000); // keep checking every 3 seconds

		// Listen to self
		peer.on('connection', function(conn) {		
    		conn.on('data', function(data){
    			console.log('received < '+data);
    			myRTC.showmsg('[from '+this.peer+'] < '+data);
    			myRTC.checkAll(); // check everytime you receive a connection
    			if(myRTC.peer.id==myRTC.server){ // if this is the server then relay to everybody else
    				for(var c in myRTC.connections){
  						var cc=myRTC.connections[c];
  						if(cc!=='all'){
  							myRTC.send(cc,data);
  						}
  					}
    				//
    			}
    		});

		});

		sendmsgtoall.onkeyup=function(evt){   // send message to all (send0)
			if(evt.keyCode==13){
				var msg=sendmsgtoall.value;
				//console.log('sending > '+msg)
				myRTC.checkAll();
				myRTC.send(myRTC.server,msg,this);
			}
		}
		sendmsgtosome.onkeyup=function(evt){   // send message to someone
			if(evt.keyCode==13){
				var msg=mid+': '+sendmsgtosome.value;
				myRTC.send(destId.value,msg,this);
				myRTC.checkAll();
			}
		}
	}
};





